
package edu.tongji.jiaowu.dao.jdbc;
 
import java.util.List;

import edu.tongji.jiaowu.dao.StudentDao;
import com.tongji.quickCRUDQ.define.Row;
import com.tongji.quickCRUDQ.model.CommonBean;
import com.tongji.quickCRUDQ.dao.jdbc.BaseDaoJdbc;
import com.tongji.quickCRUDQ.exception.FormatException;

public class StudentDaoJdbc extends BaseDaoJdbc implements StudentDao {
	String tableName="T_STUDENT";
	String beanName="student";
	
	public CommonBean createStudent(){
		CommonBean students=this.createBean(tableName,beanName);
                return students;		
	}
	
/*
	public CommonBean findStudents(String refid) {
		CommonBean students=this.createStudent();
		String sql="select * from T_STUDENT where refid=?";
		List data=this.getCommonDao().select(sql,new String[]{refid});
		students.addData(data);
                return students;
       }
*/
	public CommonBean findStudentById(String id) {
		CommonBean students=this.createStudent();
		String sql="select * from T_STUDENT where id=?";
		List data=this.getCommonDao().select(sql,new String[]{id});
		students.addData(data);
    return students;
  }
  
	public CommonBean findAllStudents() {
		CommonBean students=this.createStudent();
		String sql="select * from T_STUDENT ";
		List data=this.getCommonDao().select(sql,new String[]{});
		students.addData(data);
    return students;
  }
	public void insertStudent(CommonBean studentBean){
		List data=studentBean.getData();
		Row row=this.getRow(tableName);
		this.getCommonDao().templateBatchInsert(row,data,this.getIdGenerator());
	}

	public CommonBean findStudentByNo(String studentNo) {
		String sql="select * from v_student where student_no=?";
		List data=getCommonDao().select(sql, new String[]{studentNo});
		if(data.size()==0) throw new FormatException("无此学号:"+studentNo);
		CommonBean student=this.createBean("V_STUDENT", "vstudent");
		student.addData(data);
		return student;
	}

	public boolean getArrears(String studentNo) {
		CommonBean student=findStudentByNo(studentNo);
		String arrears=student.getPropertyAsString(0, "arrears");
		return "Y".equals(arrears);
	}

	public CommonBean newAcchountStudents() {
		String sql = "select * from t_student t where school_status='01' and not exists(" + 
		             "   select * from app_user where username=t.student_no" 
		             + ")";
		CommonBean students = this.createStudent();
		List data = getCommonDao().select(sql);
		students.addData(data);
		return students;
	}
}

